package cn.hfuu.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class MyServer {
    public static void main(String[] args) {
        try {
            ServerSocket st = new ServerSocket(9999);
            System.out.println("服务器启动了");
            while (true) {
                Socket s = st.accept();//接受连接
                System.out.println(s);//对方的ip,对方的端口（随机，30000+）
                OutputStream out = s.getOutputStream();
                InputStream in = s.getInputStream();
                //???... 转换成字符流，用 \n表示结束
                PrintWriter pw = new PrintWriter(out, true);
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                //采用问答！
                String ask = "";
                while (true) {
                    ask = br.readLine();//
                    pw.println("你问的问题，[" + ask + "],朕知道了");
                    //>>pw.flush();
                    if (ask.equals("byebye")) {
                        break;
                    }
                }
                s.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
